package com.pybox.service;

import com.pybox.common.utils.JdbcTemplateUtils;
import com.pybox.dto.TCustomersDto;
import com.pybox.entity.TCustomers;
import com.pybox.repository.TCustomersRepository;
import com.pybox.spec.TCustomersSpec;
import com.pybox.util.TimeDealUtils;
import com.pybox.util.UserUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* 描述：TCustomers Service
* @author wuyingkai
* @date 2018-09-15
*/

@Service
@Transactional
public class TCustomersService {

    @Autowired
    private TCustomersRepository repository;

    @Autowired
    private JdbcTemplateUtils jdbcTemplateUtils;

    /**
     * 创建TCustomers
     *
     * @param source
     * @return
     */
    public boolean create(TCustomers source){
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        String customerId = UserUtils.getCustomerId().toString();
        if(customerId != null){
            source.setId(null);
            source.setCustomer_id(customerId);
            source.setCtime(timestamp);
            source.setState(1);
            source.setDeleted(0);
            repository.save(source);
            return  true;
        }
        return  false;
    }

    /**
     * 根据ID删除TCustomers
     *
     * @param id
     * @return
     */
    public boolean delete(Integer id){

        repository.delete(id);

        return true;

    }

    /**
     * 更新TCustomers
     *
     * @param id
     * @param source
     * @return
     */
    public boolean update(Integer id, TCustomersDto source){

        TCustomers exist = repository.findOne(id);
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        if (null == exist){
            throw new RuntimeException("记录不存在");
        }
        source.setUtime(timestamp);
        source.setState(1);
        source.setDeleted(0);
        BeanUtils.copyProperties(source, exist, new String[]{"id"});
        repository.save(exist);

        return true;

    }

    /**
     * 根据ID查询TCustomers
     *
     * @param id
     * @return
     */
    public TCustomersDto detail(Integer id){

        TCustomers exist = repository.findOne(id);
        if (null == exist){
            throw new RuntimeException("记录不存在");
        }

        TCustomersDto target = new TCustomersDto();
        BeanUtils.copyProperties(exist, target);

        return target;

    }

    /**
     * 分页查询查询TCustomers
     *
     * @param pageNo
     * @param pageSize
     * @param paramMap
     * @return
     */
    public Page<TCustomersDto> page(Integer pageNo, Integer pageSize, Map<String, String> paramMap){

        Pageable pageable = new PageRequest(pageNo - 1, pageSize);
        Page<TCustomers> data = repository.findAll(new TCustomersSpec(paramMap), pageable);

        List<TCustomersDto> list = new ArrayList<>();
        for (TCustomers obj : data.getContent()) {
            TCustomersDto dto = new TCustomersDto();
            BeanUtils.copyProperties(obj, dto);
            list.add(dto);
        }

        Page<TCustomersDto> page = new PageImpl<>(list, pageable, data.getTotalElements());

        return page;

    }
    /**
     * 根据customer_id查询TCustomers的名称
     *
     * @param
     * @return
     */
    public TCustomersDto detail(){

        int customerId = UserUtils.getCustomerId();
        String customerName = jdbcTemplateUtils.query("SELECT name FROM `t_customers` WHERE customer_id = ?",customerId);

        if (null == customerName){
            return null;
        }

        TCustomersDto target = new TCustomersDto();
       target.setName(customerName);
        return target;

    }

    public TCustomersDto searchTcustomers() {

        int customerId = UserUtils.getCustomerId();
        String sql ="SELECT\n" +
                "\tid,\n" +
                "\tcustomer_id,\n" +
                "\tshort_name as shortName ,\n" +
                "\tname,\n" +
                "\tcountry,\n" +
                "\tprovince,\n" +
                "\tcity,\n" +
                "\taddress,\n" +
                "\tlandline,\n" +
                "\ttelephone,\n" +
                "\tphone,\n" +
                "\temail,\n" +
                "\tstate,\n" +
                "\tcontact_name as contactName,\n" +
                "\temployee_num as employeeNum,\n" +
                "\tcomments,\n" +
                "\tctime,\n" +
                "\tutime \n" +
                "FROM\n" +
                "\t`t_customers` WHERE customer_id = ?";

        TCustomersDto customersDto = (TCustomersDto) jdbcTemplateUtils.queryForObject(sql,new BeanPropertyRowMapper<>(TCustomersDto.class),customerId);
        if (null == customersDto){
            return null;
        }
//        String sql = "select num,name,age from student where id = ?";
//        RowMapper<stu> rowMapper = new BeanPropertyRowMapper<stu>(stu.class);
//        stu s = jdbcTemplate.queryForObject(sql, rowMapper,5);//最后一个参数为id值
        return customersDto;
    }
}